<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref, onMounted } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter, useRoute } from 'vue-router'
import { TableData } from '@/api/table/types'
import { useEmitt } from '@/hooks/web/useEmitt'
import { storageEdit, storageDetail, sendAdd } from '@/api/common'
import { storageType } from '@/api/common/types'
import { replace } from 'lodash-es'

const { emitter } = useEmitt()

const { push, replace } = useRouter()

const { query } = useRoute()

const { t } = useI18n()

const currentRow: any = ref(null)

const isDetail: any = ref(false)

onMounted(() => {
  console.log('---isDetail---', query.isDetail)
  isDetail.value = query.isDetail
})

const getTableDet = async () => {
  const res = await storageDetail(query.id as string)
  if (res) {
    console.log('---获取row内容---', res.data)
    currentRow.value = res.data
  }
}

getTableDet()

const addWriteRef = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWriteRef)
  console.log('---addWrite---', write?.getFormData())

  let goodsArr: any = []
  write?.getFormData()?.pieceArray.forEach((item) => {
    let obj = {
      gname: item.gname,
      worth: item.worth,
      goods_id: item.goods_id,
      piece: item.piece,
      quantity: item.quantity,
      room: item.room,
      colour: item.colour,
      length: item.length,
      width: item.width,
      high: item.high,
      weight: item.weight,
      remark: item.remark,
      total: item.total,
      price: item.price
    }
    goodsArr.push(obj)
  })

  const data = {
    goods_id: write?.getFormData()?.typeOfGoods,
    id: write?.getFormData()?.id,
    store_id: write?.getFormData()?.store_id,
    transport: write?.getFormData()?.modeOfTransport,
    placestart: write?.getFormData()?.origin,
    client_code: write?.getFormData()?.relatedCustomers,
    remark: write?.getFormData()?.productNameRemarks,
    piece: write?.getFormData()?.warehousingItems,
    entertime: Number(new Date(write?.getFormData()?.warehousingTime).valueOf()) / 1000,
    shouhuo: write?.getFormData()?.recipient,
    placeend: write?.getFormData()?.destination,
    out_date: write?.getFormData()?.out_date,
    submarket: write?.getFormData()?.submarket,
    goods: goodsArr
  } as unknown as storageType
  console.log('---采购单新增参数---', data)
  const res = await storageEdit(write?.getFormData()?.id, data)
  if (res) {
    emitter.emit('getList', 'edit')
    push('/logistics/warehousingEntry')
  }
}

// 生成出库单
const generateInvoice = async () => {
  const write = unref(addWriteRef)
  console.log('---addWrite---', write?.getFormData())
  // 1、获取填写的数据
  let goodsArr: any = []
  let formData: any = write?.getFormData() // 这个就是addWrite 里头写的方法  是用来获取填写的数据的
  console.log('---输出填写的表单数据---', formData)
  formData.pieceArray.forEach((item) => {
    let obj = {
      gname: item.gname,
      worth: item.worth,
      goods_id: item.goods_id,
      piece: item.piece,
      quantity: item.quantity,
      room: item.room,
      colour: item.colour,
      length: item.length,
      width: item.width,
      high: item.high,
      weight: item.weight,
      remark: item.remark,
      total: item.total,
      price: item.price
    }
    goodsArr.push(obj)
  })
  // 2、看看接口需要什么数据
  let apiData = {
    goods_id: formData.typeOfGoods, //货物种类
    store_id: formData.store_id, //货物仓库
    transport_text: formData.modeOfTransport, //运输方式
    placestart_text: formData.origin, //始发地
    client_code: formData.relatedCustomers, // 客户号
    remark: formData.productNameRemarks, //备注
    shouhuo: formData.recipient, //收货人
    placeend_text: formData.destination, //目的地
    // goods: JSON.stringify(goodsArr), //货物
    goods: JSON.stringify([]),
    fahuo: formData.relatedCustomers
  }
  console.log('---apiData-接口需要的数据---', apiData)
  // 3、调用接口
  let res = await sendAdd({ ...apiData })
  console.log('---新增接口调用---', res)
  if (res) {
    localStorage.setItem('needRefresh', '1')
    // setTimeout(() => {
    // replace('/logistics/invoice')
    push({ path: '/logistics/invoice', replace: true })
    // }, 300)
  }
}
</script>

<template>
  <ContentDetailWrap
    :title="!isDetail ? t('exampleDemo.edit') : '详情'"
    @back="push('/logistics/warehousingEntry')"
  >
    <!-- <Write ref="writeRef" :current-row="currentRow" /> -->
    <AddWrite ref="addWriteRef" :current-row="currentRow" />

    <template #right>
      <ElButton v-if="!isDetail" type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
      <ElButton
        type="primary"
        v-if="currentRow && currentRow.status && currentRow.status != 0"
        :loading="loading"
        @click="generateInvoice"
      >
        生成出库单
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
